// wigets/business/video/index.js
import{comments} from "../../../js/modules/mall.js"
import authorize from '../../../js/modules/authorize.js'
import {util,api} from '../../../js/util'; 
const componentBehavior = require('../../../js/common/ComponentBehavior')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    videoList:{
      type: Array,
      value: []
    },
    num:{
      type:Number,
      value:false
    },
    postId:{
      type:Number,
      value:false
    },
    pagenationObj: {
      type: Object,
      default: {}
    },
    NoLoginShowInteraction: { // 无需登录可查看发现详情
      type: Boolean,
      value: true
    }
    // Currency:String
  },

  /**
   * 组件的初始数据
   */
  data: {
    changeIndex: 0,
    discussDisplay:false,
    goodsDisplay:false,
    imgDomain: api.imgDomain,
    videoDomain:api.videoDomain,
    enableOss: api.enableOss,
    postName:'',//发布者姓名
    goodsList:[],//商品列表
  },
  observers:{
  },
  behaviors: [componentBehavior],
  /**
   * 组件的方法列表
   */
  methods: {
    slide:function(e){
      this.setData({
        changeIndex:e.detail.current
      })
      let pageObj = this.data.pagenationObj
      // 判断当前视频是不是当页最后一个
      let currentIndex = e.detail.current
      if(currentIndex === (pageObj.pageSize * pageObj.pageIndex - 1)) {
        // 判断本页是不是已经加载过数据
        if(Math.ceil(this.data.videoList / pageObj.pageSize) === pageObj.pageIndex) { 
          this.triggerEvent('loadVideoListEvent', false)
        } else {
          this.triggerEvent('loadVideoListEvent', true)
        }
      }
    },
    
    /**
    * 对互动帖子、评论点赞
    */
    bindLike:function(e){
      var that = this;
      if (!authorize.isLogin('store')) {
        authorize.redirect('store')
        return
      }
      var type = e.currentTarget.dataset.type;
      var id =  e.currentTarget.dataset.id;
      var status = e.currentTarget.dataset.status;// true - false
      var data = {Type:type,Id:id};
      var list = that.data.videoList;
      if(!status){
        //点赞
        comments.getUserFollow(data,function(content){
          if(content.Success){
            var index = e.currentTarget.dataset.index;
            list[index].LikeCount ++;
            list[index].IsSupport = true;
            that.setData({
              videoList:list
            })
          }else{
            util.alert(content.Message);
          }
        })
      }else{
        //取消点赞
        comments.getUserCancel(data,function(content) {
          if(content.Success){
            var index = e.currentTarget.dataset.index;
            list[index].LikeCount --;
            list[index].IsSupport = false;
            that.setData({
              videoList:list
            })
          }else{
            util.alert(content.Message);
          }
        })
      }
    },
    /**
   * 展开----收起
   */
    bindOpen:function(e){
      var that = this;
      var index = e.currentTarget.dataset.index;
      var list = that.data.videoList;
      list[index].open = !list[index].open;
      that.setData({
        videoList:list
      })
    },
    /**
     * 留言
     */
    btnMess:function(e){
      if (!authorize.isLogin('store')) {
        authorize.redirect('store')
        return
      }
      // console.log(e.currentTarget.dataset.id,'----');
      this.setData({discussDisplay:true,postName:e.currentTarget.dataset.name,postId:e.currentTarget.dataset.id})
    },

    /**
     * 推荐商品
     */
    btnGoods:function(e){
      if (!authorize.isLogin('store')) {
        authorize.redirect('store')
        return
      }
      this.setData({goodsDisplay:true,goodsList:e.currentTarget.dataset.goodlist})
    },
  }
})